#include "ADUGamePlayScene.h"
#include "ADUDuck.h"
#include "ADUFlow.h"

USING_NS_CC;

Scene* GamePlayLayer::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = GamePlayLayer::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool GamePlayLayer::init()
{
    //////////////////////////////
    // 1. super init first
	mt = 0;
	mAllowFlow = false;
    if ( !Layer::init() )
    {
        return false;
    }
    
    mWorld = GameManager::getInstance()->mWorld;

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    float centerX = origin.x + visibleSize.width/2;
    float centerY = origin.y + visibleSize.height/2;
    // Add Menu
	mExitSprite = Sprite::create(IMG_EXIT_PNG);
	mPauseSprite = Sprite::create(IMG_PAUSE_PNG);
	mSoundSprite = Sprite::create(IMG_SOUND_PNG);
	mMusicSprite = Sprite::create(IMG_MUSIC_PNG);

	float btn_width = mExitSprite->getContentSize().width;
	float btn_height = mExitSprite->getContentSize().height;

	mExitSprite->setPosition(origin.x + btn_width, origin.y + visibleSize.height - btn_height/2);
	mPauseSprite->setPosition(origin.x + visibleSize.width - btn_width, origin.y + visibleSize.height - btn_height/2);
	mSoundSprite->setPosition(origin.x + visibleSize.width - 2*btn_width,origin.y + visibleSize.height - btn_height/2);
	mMusicSprite->setPosition(origin.x + visibleSize.width - 3*btn_width,origin.y + visibleSize.height - btn_height/2);

	this->addChild(mExitSprite, Z_MENU);
	this->addChild(mPauseSprite, Z_MENU);
	this->addChild(mMusicSprite, Z_MENU);
	this->addChild(mSoundSprite, Z_MENU);
	

	
    // Add background
    mBackground = Sprite::create(IMG_BACKGROUND);
    mBackground->setPosition(centerX, centerY);
    this->addChild(mBackground, Z_BACKGROUND);

	// Add Pipe
	mPipe = new Pipe();
	mPipe->createPipe(this);
	Vec2 pipeHeadPos = mPipe->getPipeHeadPosition();

	// Add Fluid
	mFlow = new Flow(pipeHeadPos);
	mFlow->setPosition(origin);
	this->addChild(mFlow, Z_FLUID);

	// Add Duck
	mDuck = Duck::create(centerX,centerY,IMG_DUCK);
	this->addChild(mDuck, Z_DUCK);
    
    // Add Water

    mWater = new Water(200,250.0f);
    mWater->SetupColor(Color4F(0, 0, 1, 0.5), Color4F(0, 0, 1, 0.5));
    
   // mWater->Splash(-50.0f,200);

    this->addChild(mWater->GetDrawer(),1);


	GLESDebugDraw* drawer = new GLESDebugDraw(PTM_RATIO);
	drawer->SetFlags(GLESDebugDraw::e_controllerBit);
	drawer->SetFlags(GLESDebugDraw::e_shapeBit);

	mWorld->SetDebugDraw(drawer);


    
    // Basin test
	Basin* basin = Basin::CreateBasin();
	basin->setPosition(ccp(centerX, centerY));
	this->addChild(basin, Z_DUCK);


	// Controller test - start

	mWater->AddBody(basin->GetBody());
	mWater->AddBody(mDuck->GetBody());

















	// Controller test - end


	// Touch enable
	mTouchListener = EventListenerTouchOneByOne::create();
//	mTouchListener->setSwallowTouches(true);
	mTouchListener->onTouchBegan = CC_CALLBACK_2(GamePlayLayer::onTouchBegan, this);
	mTouchListener->onTouchMoved = CC_CALLBACK_2(GamePlayLayer::onTouchMoved, this);
	mTouchListener->onTouchEnded = CC_CALLBACK_2(GamePlayLayer::onTouchEnded, this);
	mTouchListener->onTouchCancelled = CC_CALLBACK_2(GamePlayLayer::onTouchCancelled, this);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(mTouchListener, this);

	this->scheduleUpdate();

    return true;
}




void GamePlayLayer::update(float dt) {

	// Temp code for water splash - start
	if (mt == 0){

	int i = rand()%200;
	mWater->Splash(-20.0f,i);
	}
	mt+=dt;
	if (mt > 3) mt = 0;
	// Temp code for water splash - end

	GameManager::getInstance()->Step(dt);
	if(mAllowFlow)
		mFlow->update(dt);
	mWater->OnUpdate(dt);
}

void GamePlayLayer::OnExitClicked() {
	log("Exit");
}
void GamePlayLayer::OnPauseClicked() {
	log("Pause");
}
void GamePlayLayer::OnSoundClicked() {
	log("Sound");
}
void GamePlayLayer::OnMusicClicked() {
	log("Music");
}
bool GamePlayLayer::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) {

	if(mExitSprite->getBoundingBox().containsPoint(touch->getLocation())) {
		OnExitClicked();
	} else if(mPauseSprite->getBoundingBox().containsPoint(touch->getLocation())) {
		OnPauseClicked();
	} else if(mSoundSprite->getBoundingBox().containsPoint(touch->getLocation())) { 
		OnSoundClicked();
	} else if(mMusicSprite->getBoundingBox().containsPoint(touch->getLocation())) {
		OnMusicClicked();
	} else {
		if(!mAllowFlow)
			mAllowFlow = true;
		mFlow->mEmitter.speedUp(5.0f);
	}


}
void GamePlayLayer::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event) {

}
void GamePlayLayer::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event) {
	if(mAllowFlow)
		mAllowFlow = false;
}
void GamePlayLayer::onTouchCancelled(cocos2d::Touch *touch, cocos2d::Event *event) {

}


void GamePlayLayer::draw(Renderer *renderer, const Mat4 &transform,
		uint32_t flags) {
	CCNode::draw(renderer,transform,flags);
	//GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION);
	kmGLPushMatrix();
	//mWorld->DrawDebugData();
	kmGLPopMatrix();
}
